package cn.iocoder.yudao.module.handguard.service.journalism;

import cn.iocoder.yudao.module.handguard.dal.dataobject.product.ProductDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.user.MiniUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.journalism.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.journalism.JournalismDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.journalism.JournalismConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.journalism.JournalismMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 新闻管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class JournalismServiceImpl implements JournalismService {

    @Resource
    private JournalismMapper journalismMapper;

    @Resource
    private MiniUserMapper miniUserMapper;

    @Override
    public Long createJournalism(JournalismCreateReqVO createReqVO) {
        // 插入
        JournalismDO journalism = JournalismConvert.INSTANCE.convert(createReqVO);
        journalismMapper.insert(journalism);
        // 返回
        return journalism.getId();
    }

    @Override
    public void updateJournalism(JournalismUpdateReqVO updateReqVO) {
        // 校验存在
        validateJournalismExists(updateReqVO.getId());
        // 更新
        JournalismDO updateObj = JournalismConvert.INSTANCE.convert(updateReqVO);
        journalismMapper.updateById(updateObj);
    }

    @Override
    public void deleteJournalism(Long id) {
        // 校验存在
        validateJournalismExists(id);
        // 删除
        journalismMapper.deleteById(id);
    }

    private void validateJournalismExists(Long id) {
        if (journalismMapper.selectById(id) == null) {
            throw exception(JOURNALISM_NOT_EXISTS);
        }
    }

    @Override
    public JournalismDO getJournalism(Long id) {
        return journalismMapper.selectById(id);
    }

    @Override
    public List<JournalismDO> getJournalismList(Collection<Long> ids) {
        return journalismMapper.selectBatchIds(ids);
    }

    @Override
    public List<JournalismDO> getJournalismList() {
        LambdaQueryWrapper<JournalismDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(JournalismDO::getStatus,0);
        wrapper.orderByDesc(JournalismDO::getReleaseTime);
//        wrapper.last("limit 2");
        return journalismMapper.selectList(wrapper);
    }

    @Override
    public PageResult<JournalismDO> getJournalismPage(JournalismPageReqVO pageReqVO) {
        PageResult<JournalismDO> pageResult = journalismMapper.selectPage(pageReqVO);
        if (pageResult.getList().size()>0){
            List<JournalismDO> productDOList = pageResult.getList();
            for (JournalismDO j : productDOList){
                //
               UserDO userDO = miniUserMapper.selectById(j.getCreator());
               j.setCreator(userDO.getName());
            }
            pageResult.setList(productDOList);
        }
        return pageResult;
    }

    @Override
    public List<JournalismDO> getJournalismList(JournalismExportReqVO exportReqVO) {
        return journalismMapper.selectList(exportReqVO);
    }

}
